package com.youxuan.ssyx.acl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youxuan.ssyx.acl.mapper.RoleMapper;
import com.youxuan.ssyx.acl.service.AdminRoleService;
import com.youxuan.ssyx.acl.service.RoleService;
import com.youxuan.ssyx.model.acl.AdminRole;
import com.youxuan.ssyx.model.acl.Role;
import com.youxuan.ssyx.vo.acl.RoleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private AdminRoleService adminRoleService;

    @Override
    public IPage<Role> selectRolePage(Page<Role> pageParam, RoleQueryVo roleQueryVo) {

        // 获取条件值
        String roleName = roleQueryVo.getRoleName();

        // 创建 mp 条件对象
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();

        // 判断条件值是否是空
        if(!StringUtils.isEmpty(roleName)) {
            wrapper.like(Role::getRoleName,roleName);
        }

        // 调用方法实现分页查询
        return baseMapper.selectPage(pageParam,wrapper);
    }

    @Override
    public Map<String, Object> getRoleByAdminId(Long adminId) {
        // 查询所有角色
        List<Role> allRoleList = baseMapper.selectList(null);

        // 根据用户 ID 查询用户角色关系表，查询用户分配角色 id 列表
        LambdaQueryWrapper<AdminRole> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(AdminRole::getAdminId,adminId);

        List<AdminRole> adminRoleList = adminRoleService.list(wrapper);

        List<Long> roleIdsList = adminRoleList.stream().map(AdminRole::getRoleId).collect(Collectors.toList());

        List<Role> assignRoleList = new ArrayList<>();

        for (Role role:allRoleList) {
            if(roleIdsList.contains(role.getId())) {
                assignRoleList.add(role);
            }
        }

        Map<String,Object> result = new HashMap<>();
        result.put("allRolesList", allRoleList);
        result.put("assignRoles", assignRoleList);
        return result;
    }

    @Override
    public void saveAdminRole(Long adminId, Long[] roleIds) {
        // 删除用户已经分配过的角色数据
        // 根据用户 id 删除 admin_role表中对应的数据
        LambdaQueryWrapper<AdminRole> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(AdminRole::getAdminId,adminId);

        adminRoleService.remove(wrapper);

        // 重新分配
        List<AdminRole> list = new ArrayList<>();

        for (Long roleId:roleIds) {
            AdminRole adminRole = new AdminRole();
            adminRole.setAdminId(adminId);
            adminRole.setRoleId(roleId);
            list.add(adminRole);
        }

        adminRoleService.saveBatch(list);
    }
}
